﻿namespace MDXStudio
{
    using System;
    using System.Diagnostics;

    internal class ASPerfCounter
    {
        private PerformanceCounter m_CounterObject;
        private string m_Description;
        private int m_Value;

        public ASPerfCounter(string server, string version, string category, string name, string description)
        {
            try
            {
                string str2;
                this.m_CounterObject = new PerformanceCounter();
                string[] strArray = server.Split(new char[] { '\\' });
                if ((strArray.Length > 2) || (strArray.Length < 1))
                {
                    throw new Exception("Invalid server name");
                }
                string machineName = strArray[0];
                if ((machineName.Equals(".") || machineName.Equals("local")) || machineName.Equals("localhost"))
                {
                    machineName = Environment.MachineName;
                }
                this.m_CounterObject.MachineName = machineName;
                if (1 == strArray.Length)
                {
                    str2 = string.Format("MSAS {0}:{1}", version, category);
                }
                else
                {
                    str2 = string.Format("MSOLAP${0}:{1}", strArray[1], category);
                }
                this.m_CounterObject.CategoryName = str2;
                this.m_CounterObject.CounterName = name;
                this.m_Description = description;
                this.m_CounterObject.NextValue();
            }
            catch
            {
                this.m_CounterObject = null;
            }
        }

        public void Finish()
        {
            try
            {
                if (this.m_CounterObject != null)
                {
                    this.m_Value = ((int) this.m_CounterObject.NextValue()) - this.m_Value;
                }
            }
            catch
            {
            }
        }

        public string GetDescription()
        {
            return this.m_Description;
        }

        public string GetValueText()
        {
            if (this.m_Value != -1)
            {
                return this.m_Value.ToString();
            }
            return "?";
        }

        public void Start()
        {
            try
            {
                if (this.m_CounterObject != null)
                {
                    this.m_Value = (int) this.m_CounterObject.NextValue();
                }
            }
            catch
            {
            }
        }
    }
}

